﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using ProxyHero.Entity;

namespace ProxyHero.TabPages
{
    public partial class frmTestProxy : Form
    {
        public frmTestProxy()
        {
            InitializeComponent();
        }

        private void tsbPaste_Click(object sender, EventArgs e)
        {
            this.rtbProxyList.Paste();
            Format();
        }

        private void Format()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < this.rtbProxyList.Lines.Length; i++)
            {
                string line = this.rtbProxyList.Lines[i];
                Regex regex = new Regex(@"(?<IP>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))\:(?<Port>\d{1,5})");
                MatchCollection matchs = regex.Matches(line);
                if (matchs.Count == 1)
                {
                    string ip = matchs[0].Groups["IP"].Value;
                    string port = matchs[0].Groups["Port"].Value;
                    sb.Append(ip);
                    sb.Append(":");
                    sb.Append(port);
                    sb.Append("\n");
                }
                else
                {
                    regex = new Regex(@"(?<IP>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))\s(?<Port>\d{1,5})");
                    matchs = regex.Matches(line);
                    if (matchs.Count == 1)
                    {
                        string ip = matchs[0].Groups["IP"].Value;
                        string port = matchs[0].Groups["Port"].Value;
                        sb.Append(ip);
                        sb.Append(":");
                        sb.Append(port);
                        sb.Append("\n");
                    }
                    else
                    {
                        regex = new Regex(@"(?<IP>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))\：(?<Port>\d{1,5})");
                        matchs = regex.Matches(line);
                        if (matchs.Count == 1)
                        {
                            string ip = matchs[0].Groups["IP"].Value;
                            string port = matchs[0].Groups["Port"].Value;
                            sb.Append(ip);
                            sb.Append(":");
                            sb.Append(port);
                            sb.Append("\n");
                        }
                    }
                }
            }
            this.rtbProxyList.Text = sb.ToString();
        }

        private void rtbProxyList_TextChanged(object sender, EventArgs e)
        {
            if (this.rtbProxyList.Lines.Length > 0)
            {
                this.tsslStatus.Text = "共" + this.rtbProxyList.Lines.Length + "条";
            }
            else
            {
                this.tsslStatus.Text = "代理格式应为127.0.0.1:80";
            }
        }

        private void tsbTest_Click(object sender, EventArgs e)
        {
            List<ProxyEntity> list = new List<ProxyEntity>();
            for (int i = 0; i < this.rtbProxyList.Lines.Length; i++)
            {
                string line = this.rtbProxyList.Lines[i];
                Regex regex = new Regex(@"(?<IP>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))\:(?<Port>\d{1,5})");
                MatchCollection matchs = regex.Matches(line);
                if (matchs.Count == 1)
                {
                    ProxyEntity pe = new ProxyEntity();
                    pe.Ip = matchs[0].Groups["IP"].Value;
                    pe.Port = int.Parse(matchs[0].Groups["Port"].Value);
                    list.Add(pe);
                }
            }

            if (list.Count > 0)
            {
                Config.ProxyData.Clear();
                foreach (ProxyEntity pe in list)
                {
                    lock (Config.ProxyData)
                    {
                        if (!Config.ProxyData.Rows.Contains(new object[] { pe.Ip, pe.Port}))
                        {
                            Config.ProxyData.Rows.Add(new object[] { pe.Ip, pe.Port, "", "HTTP", "", 999999 });
                        }
                    }
                }

                Config.MainForm.tsmiViewProxyList_Click(sender, e);

                foreach (Crownwood.Magic.Controls.TabPage page in Config.MainForm.tabControlMain.TabPages)
                {
                    if (page.Control.Name == "frmProxy")
                    {
                        Config.MainForm.tabControlMain.SelectedTab = page;
                        frmProxy formProxy = (frmProxy)page.Control;
                        formProxy.TestAll();
                        break;
                    }
                }

                Config.ProxyViewForm.BindDataGrid();
            }
        }

        private void tsbDeleteAll_Click(object sender, EventArgs e)
        {
            this.rtbProxyList.Clear();
        }
    }
}
